package com.zzsy.carosshopprojects.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.king.base.adapter.ViewHolderRecyclerAdapter;
import com.king.base.adapter.holder.ViewHolder;
import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.bean.ReceiptBean;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.FormatTimeUtil;
import com.zzsy.carosshopprojects.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

public class ShoppingCartAdapter extends ViewHolderRecyclerAdapter<ReceiptBean.UnInvoiceOrderDTOsBean> {
    private Context context;
    private List<ReceiptBean.UnInvoiceOrderDTOsBean> mAllData;
    private CheckBox mCheckboxAll;
    private TextView mTvShopcartTotal;
    private TextView mTvCount;
    private LinearLayout tv_checkbox_all;

    public ShoppingCartAdapter(Context context, List<ReceiptBean.UnInvoiceOrderDTOsBean> allData, int layoutId, CheckBox checkboxAll, LinearLayout tv_checkbox_all, TextView tvShopcartTotal, TextView tvCount) {
        super(context, allData, layoutId);
        this.context = context;
        this.mAllData = allData;
        this.mCheckboxAll = checkboxAll;
        this.tv_checkbox_all = tv_checkbox_all;
        this.mTvShopcartTotal = tvShopcartTotal;
        this.mTvCount = tvCount;
        showCount();
        ShowTotaLPrice();
        //设置点击事件
        //设置点击事件
        setListener();
        //校验是否全选
        checkAll();
    }

    private void setListener() {
        setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                try {
                    //根据位置找到对应的bean
                    ReceiptBean.UnInvoiceOrderDTOsBean goodsBean = mAllData.get(position);
                    //设置取反状态
                    goodsBean.setSelected(!goodsBean.isSelected());
                    //刷新数据
                    notifyItemChanged(position);
                    //校验是否全选
                    checkAll();
                    //重新计算
                    ShowTotaLPrice();
                    //重新统计个数
                    showCount();
                } catch (Exception e) {
                    Toast.makeText(context, "数据异常~", Toast.LENGTH_SHORT).show();
                }

            }
        });

        //设置全选费全选的点击事件
        tv_checkbox_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //得到状态
                boolean isChecked = mCheckboxAll.isChecked();
                if (isChecked) {
                    mCheckboxAll.setChecked(false);
                } else {
                    mCheckboxAll.setChecked(true);
                }
                //根据状态设置全选和非全选

                checkAll_none(!isChecked);

                //计算总价格
                ShowTotaLPrice();
                //计算个数
                showCount();
            }
        });
    }

    /**
     * 总合计
     */
    public void ShowTotaLPrice() {
        mTvShopcartTotal.setText("￥" + getTtotalPrices());
    }

    private void showCount() {
        mTvCount.setText(getCount());
    }

    //设置个数
    private String getCount() {
        int count = 0;
        if (mAllData != null && mAllData.size() > 0) {
            for (int i = 0; i < mAllData.size(); i++) {
                ReceiptBean.UnInvoiceOrderDTOsBean goodsBean = mAllData.get(i);
                if (goodsBean.isSelected()) {
                    count++;
                }
            }
        }
        return String.valueOf(count);
    }

    /**
     * 计算总价格
     */
    private String getTtotalPrices() {
        double totalPrice = 0;
        if (mAllData != null && mAllData.size() > 0) {
            for (int i = 0; i < mAllData.size(); i++) {
                ReceiptBean.UnInvoiceOrderDTOsBean goodsBean = mAllData.get(i);
                if (!StringUtils.isEmpty(goodsBean.getInvoiceAmount()) && goodsBean.isSelected()) {
                    totalPrice = totalPrice + Double.parseDouble(goodsBean.getInvoiceAmount()) / 100;
                }
            }
        }
        return ConverterHelper.toBigDecimal(totalPrice).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * 设置全选和非全选
     */
    public void checkAll_none(boolean isChecked) {
        if (mAllData != null && mAllData.size() >= 0) {
            for (int i = 0; i < mAllData.size(); i++) {
                ReceiptBean.UnInvoiceOrderDTOsBean goodsBean = mAllData.get(i);
                goodsBean.setSelected(isChecked);
                notifyItemChanged(i);
            }

        }
    }

    /**
     * 校验是否全选
     */
    public void checkAll() {
        if (mAllData != null && mAllData.size() >= 0) {
            int number = 0;
            for (int i = 0; i < mAllData.size(); i++) {
                ReceiptBean.UnInvoiceOrderDTOsBean goodsBean = mAllData.get(i);
                if (!goodsBean.isSelected()) {
                    //非全选
                    mCheckboxAll.setChecked(false);
                } else {
                    //选中的
                    number++;
                }
            }
            if (number == mAllData.size()) {
                //全选
                mCheckboxAll.setChecked(true);
            }
        } else {
            mCheckboxAll.setChecked(false);
        }
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.adapter_shoppingcart, null);
        ScreenAdapterTools.getInstance().loadView(view);
        return view;
    }

    @Override
    public void bindViewDatas(ViewHolder helper, ReceiptBean.UnInvoiceOrderDTOsBean goodsBean, int positions) {
        //设置数据
        CheckBox cb_gov = helper.getView(R.id.cb_gov);
        RelativeLayout rlShow = helper.getView(R.id.rl_show);
        cb_gov.setChecked(goodsBean.isSelected());
        helper.setText(R.id.tv_ordernum, goodsBean.getOrderNumber());
        double money = ConverterHelper.toBigDecimal(goodsBean.getInvoiceAmount()).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        helper.setText(R.id.tv_money, "￥" + money);
        helper.setText(R.id.tv_oils, goodsBean.getOilNum() + "");
        String orderCreateTime = goodsBean.getOrderCreateTime();
        if (!StringUtils.isEmpty(orderCreateTime)) {
            helper.setText(R.id.tv_order_time, FormatTimeUtil.formatTimeStamp2DateStr(String.valueOf(Long.parseLong(orderCreateTime) / 1000), "yyyy-MM-dd HH:mm:ss"));
        }
        helper.setText(R.id.tv_station, goodsBean.getStoreName());
        String type = goodsBean.getType();
        if ("1".equals(type)) {
            rlShow.setVisibility(View.VISIBLE);
        } else if ("2".equals(type)) {
            rlShow.setVisibility(View.GONE);
        }

        helper.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(positions);
                }
            }
        });

    }

    /**
     * 监听者
     */
    public interface OnItemClickListener {
        /**
         * 点击某一条的时候
         */
        void onItemClick(int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
